package cn.wjssl.trigger.job.springJob;

import cn.wjssl.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import cn.wjssl.domain.strategy.service.IRaffleStock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 定时同步Mysql 奖品库存
 */
@Slf4j
@Component
public class UpdateAwardStockJob {

    @Resource
    private IRaffleStock stock;

    @Scheduled(cron = "0/5 * * * * ?")
    public void exec() {
        try {
            //log.info("定时任务，更新奖品消耗库存【延迟队列获取，降低对数据库的更新频次，不要产生竞争】");
            // 从阻塞队列中获得扣减库存的奖品
            StrategyAwardStockKeyVO strategyAwardStockKeyVO = stock.takeQueueValue();
            // 如果当前没有扣减的库存, 就结束任务, 等下一个5秒再看
            if (null == strategyAwardStockKeyVO) return;
            log.info("定时任务，更新奖品消耗库存 strategyId:{} awardId:{}", strategyAwardStockKeyVO.getStrategyId(), strategyAwardStockKeyVO.getAwardId());
            stock.updateStrategyAwardStock(strategyAwardStockKeyVO.getStrategyId(), strategyAwardStockKeyVO.getAwardId());
        }catch (Exception e){
            log.error("定时任务，更新奖品消耗库存失败", e);
        }
    }
}
